(function($, gmwk) {

    function Task(Options) {
  
        // console.info("Options", Options);
    
        var self = this;
        var sClassName = Options.sClassName;
        var jqContainer = Options.jqObjectContainer;
        var jqTemplates = jqContainer.find(".Task_template");
        var jqList = jqContainer.find(".Task_list");
        var jqForm = jqContainer.find(".Task_form");
        var fListTemplate;
        var fFormTemplate;
        
        jqContainer.click(fClick);
        
        // console.info("---------> Task constructed");
        
        this.getClassName = function() {
            return sClassName;
        }
        
        this.route = function(Fragment) {
            // Default is list
            if (Fragment.action === undefined) {
                fList();
            // Do action 
            } else {
                switch (Fragment.action) {
                  case "list":
                      fList();
                      break;
                  case "add":
                      fAdd();
                      break;
                  case "edit":
                      fEdit(Fragment.nCID);
                      break;
                }
            }
        }
            
        function fClick(e) {
            var jqTarget = $(e.target);
            if (gmwk.closestObjectContainer(jqTarget) != jqContainer.attr("id")) {
                return;
            }
            console.info("fClick");
            //console.info("fClick");
            // Edit
            if (
                jqTarget.closest("div").is("") &&
                jqTarget.is(":not(.Task_delete)")
            ) {
                fLoad(
                  {
                      action: "edit",
                      nCID: fGetCID(jqTarget.closest(".Task_list_row"))
                  }
                );
            // Save
            } else if (jqTarget.is(".Task_save")) {
                fSave();
            }
        }
        
        function fGetCID(jqListRow) {
            return jqListRow.find(".Task_nCID").text();
        }
        
        function fLoad(Options, nMode) {
            if (typeof nMode == undefined) {
              nMode = gmwk.Router.PARAMS_OVERRIDE_STATE();
            }
            gmwk.Router.load(
                $.extend(
                    {
                        stage: "Dashboard",
                        menu: "Task"
                    },
                    Options
                ),
                nMode
            );
        }
        
        function fList() {
            gmwk.API.request(
                "Task/list",
                {},
                function(Data) {
                    fRenderList(Data);
                }
            );
        }
        
        function fEdit(nCID) {
            if (
                (typeof nCID === "undefined") ||
                (nCID === "")
            ) {
                alert("Cannot edit, missing nCID");
                return;
            }
            gmwk.API.request(
                "Task/details",
                {
                    Params: {
                        nCID: nCID
                    }
                },
                function(Data) {
                    fRenderForm(Data);
                }
            );
        }
        
        function fAdd() {
            fRenderForm(
                {
                    aTask: [
                        {
                            nCID: 0,
                            sHeading: "",
                            sDescription: ""
                        }
                    ]
                }
            );
        }
        
        function fDelete(nCID) {
            if (
                (typeof nCID === "undefined") ||
                (nCID === "")
            ) {
                alert("Cannot delete, missing nCID");
                return;
            }
            gmwk.API.request(
                "Task/delete",
                {
                    Params: {
                        nCID: nCID
                    }
                },
                function(Data) {
                    console.info("here", gmwk.Router.PARAMS_REPLACE_STATE());
                    fLoad({action: "list"}, gmwk.Router.PARAMS_REPLACE_STATE());
                }
            );
        }
        
        function fSave() {
            gmwk.API.request(
                "Task/save",
                {
                    jqForm: jqForm.find("form")
                },
                function(Data) {
                    fLoad({action: "list"});
                }
            );
        }
        
        function fRenderList(Data) {
            var Directive = {
                "div.Task_list_row": { // Find ul with class Task_list_row
                    "Task<-aTasks": { // Iterate Task over array aTask
                        "span.Task_nCID": "Task.nCID",
                        // Assign value of Task.sHeading to li with class Task_sHeading
                        "span.Task_sHeading": "Task.sHeading",
                        "span.Task_sDescription": "Task.sDescription",
                        // Assign return value of function to href property
                        "a.Task_delete@href": function(Options) {
                            return "#stage=Dashboard&menu=Task&action=delete&nCID=" + Options.item.nCID;
                        }
                    }
                }
            }
            // Compile the template to make it re-useable
            if (typeof fListTemplate == "undefined") {
                fListTemplate = jqList.find(".Task_list_template")
                    .compile(Directive);
            }
            // Render
            jqList.find(".Task_list_template")
                .render(Data, fListTemplate);
            // Hide other templates and show list
            jqTemplates.hide();
            jqList.show();
        }
        
        function fRenderForm(Data) {
            var Task = Data.aTask[0];
            var Directive = {
                ".Task_nCID@value": "nCID",
                ".Task_sHeading": "sHeading",
                ".Task_sDescription@value": "sDescription"
            }
            // Compile the template
            if (typeof fFormTemplate == "undefined") {
                fFormTemplate = jqForm.find(".Task_form_template")
                    .compile(Directive);
            }
            // Render
            jqForm.find(".Task_form_template")
                .render(Task, fFormTemplate);
            // Hide other templates and show form
            jqTemplates.hide();
            jqForm.show();
        }
      
    }
  
    gmwk.classes.Task = Task;

})(jQuery, window.gmwk);
